package com.kunyi.web.service.identity.code.service;


import com.kunyi.web.service.identity.code.entity.KyIdResult;
import com.kunyi.web.service.identity.code.service.KyIDGen;
import com.kunyi.web.service.identity.code.service.impl.SegmentIDGenImpl;
import com.kunyi.web.service.identity.db.service.ILeftAllocService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SegmentService {

    KyIDGen idGen;

    public SegmentService(ILeftAllocService leftAllocService)  {
        SegmentIDGenImpl segmentIDGen = new SegmentIDGenImpl();
        segmentIDGen.setDao(leftAllocService);
        idGen=segmentIDGen;
        if (idGen.init()) {
          log.info("初始化成功");
        } else {
            throw new RuntimeException("Segment Service 初始化失败");
        }
    }

    public KyIdResult getId(String key) {
        return idGen.get(key);
    }

    public SegmentIDGenImpl getIdGen() {
        if (idGen instanceof SegmentIDGenImpl) {
            return (SegmentIDGenImpl) idGen;
        }
        return null;
    }
}
